package com.framework.loippi.service.impl;

import com.framework.loippi.dao.DoctorDao;
import com.framework.loippi.dao.SubscribeDao;
import com.framework.loippi.entity.Doctor;
import com.framework.loippi.entity.OrderItem;
import com.framework.loippi.entity.Subscribe;
import com.framework.loippi.service.SubscribeService;
import com.framework.loippi.support.APIPrincipal;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * SERVICE - Subscribe(预约表)
 * 
 * @author wmj
 * @version 2.0
 */
@Service
public class SubscribeServiceImpl extends GenericServiceImpl<Subscribe, Long> implements SubscribeService {
	
	@Autowired
	private SubscribeDao subscribeDao;
	@Autowired
	private DoctorDao doctorDao;
	
	
	@Autowired
	public void setGenericDao() {
		super.setGenericDao(subscribeDao);
	}

	public List<Subscribe> findListByPage(Object parameter){
		return subscribeDao.findListByPage(parameter);
	}

	public Long deletes(Object parameter){
		return subscribeDao.deletes( parameter);
	}

	@Override
	public Long findSubNumByDate(Object createTime) {
		return subscribeDao.findSubNumByDate(createTime);
	}

	@Override
	public List<Subscribe> findFinished(Long patientId, Integer currentPage, Integer pageSize) {

		return subscribeDao.findFinished(patientId,currentPage,pageSize);
	}

	@Override
	public List<Subscribe> findAllByPatientId(Long patientId, Integer currentPage, Integer pageSize) {
		return subscribeDao.findAllByPatientId(patientId,currentPage,pageSize);
	}

	@Override
	public Integer findAllByPatientId(Long patientId) {
		return subscribeDao.findAllByPatientIdCount(patientId);
	}

	@Override
	public List<Subscribe> findDoing(Long patientId) {

		return subscribeDao.findDoing(patientId);
	}

	@Override
	public Integer findAllFinished(Long patientId) {
		return subscribeDao.findAllFinished(patientId);
	}

	@Override
	public List<Subscribe> findDid(Long patientId) {
		return subscribeDao.findDid(patientId);
	}

	@Override
	public List<Subscribe> findSicking(Long patientId) {
		return subscribeDao.findSicking(patientId);
	}

	@Override
	public List<Subscribe> findSick(Long patientId) {
		return subscribeDao.findSick(patientId);
	}

	@Override
	public String buildSubscribeId(Long doctorId, Long departmentId, Long patientId, Date subscribeTime, Integer status, Integer type) throws ParseException {
		synchronized (TOrderServiceImpl.class) {
			Subscribe subscribe = new Subscribe();
			subscribe.setDoctorId(doctorId);
			subscribe.setDepartId(departmentId);
			subscribe.setPatientId(patientId);
			subscribe.setSubscribeTime(subscribeTime);
			subscribe.setType(type);
			BigDecimal registration = new BigDecimal("0.00");
			Doctor doctor = doctorDao.find(doctorId);
			if (doctor != null) {
				registration = registration.add(new BigDecimal(doctor.getRegistration()));
				subscribe.setRegistration(registration);
			}
			Long insert = subscribeDao.insert(subscribe);
			if (insert > 0) {

				return subscribe.getId() + "";
			}
			throw new RuntimeException("下单失败！");
		}
	}

	@Override
	public List<Subscribe> findByDateAndDoctorId(@Param(value="subscribeDate")String subscribeDate, @Param(value="doctorId")Long doctorId) {
		return subscribeDao.findByDateAndDoctorId(subscribeDate, doctorId);
	}

	@Override
	public String findSubscribeText() {
		return subscribeDao.findSubscribeText();
	}
}
